// index.js
// 获取应用实例
import {
  post,
  get
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
const app = getApp();

Page({
  data: {
    tabs: ['可使用', '已使用', '已过期'],
    // 1 可使用 2 已使用 3 已过期
    couponList: [],
    currentIndex: 0,
    shopInfo: {},
    userInfo: {}
  },
  onLoad() {
    const id = wx.getStorageSync('currentShop');
    const name = wx.getStorageSync('currentShopName');
    const userInfo = wx.getStorageSync('userInfo');
    this.setData({
      shopInfo: {
        id,
        name
      },
      userInfo
    })
  },
  onShow() {
    this.getReceives(this.data.currentIndex);
  },
  getReceives(status) {
    get(APIS.receive, {
      shop: this.data.shopInfo.id,
      used: status,
      luid: this.data.userInfo.id
    }).then(res => {
      this.setData({
        couponList: res.data
      })
    })
  },
  // getDiscountList(idx) {
  //   const shop = wx.getStorageSync('currentShop');
  //   get(APIS.discount, {
  //     shop,
  //     used: idx < 2 ? idx : undefined,
  //     expired: idx === 2 ? 1 : 0
  //   }).then(res => {
  //     const list = res.data;
  //     this.setData({
  //       couponList: list.map(x => {
  //         return {
  //           ...x,
  //           time: `${x.start} ~ ${x.end}`,
  //           status: idx + 1
  //         }
  //       })
  //     });
  //     console.log(list);
  //   })
  // },
  handleChange(e) {
    const {
      index
    } = e.target.dataset;
    this.setData({
      currentIndex: index
    });
    this.getReceives(index);
  },
  handleTask(e) {
    let item = e.currentTarget.dataset.item;
    // wx.navigateTo({
    //   url: '/subPackagePay/pages/task'
    // })
    wx.scanCode({
      success (res) {
        wx.navigateTo({
          url: res.result
        })
      }
    })
  },
  onShareAppMessage() {
    return getApp().globalData.shareMessage
  }
})